package vsvm.data.filter.ranking.rankingStuff;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Threshold implements Serializable
{

	private static final long serialVersionUID = 100L;
	private boolean threshold = true;
	private double thresholdValue = 0.05;
	private int leave = 10;
	
	public int getLeave() {
		return leave;
	}
	public void setLeave(int leave) {
		this.leave = leave;
	}
	public boolean isThreshold() {
		return threshold;
	}
	public void setThreshold(boolean threshold) {
		this.threshold = threshold;
	}
	public double getThresholdValue() {
		return thresholdValue;
	}
	public void setThresholdValue(double thresholdValue) {
		this.thresholdValue = thresholdValue;
	}
	
	public void afterThreshold(double[] values, int classIndex, int[] sel, double[] val)
	{	
		if(threshold)
		{
			for (int i = 0; i < sel.length; ++i)
			{
				if(values[i] >= thresholdValue || classIndex == i)
				{
					sel[i] = -1;
					val[i] = values[i];
				} 
				else 
				{
					sel[i] = i;
					val[i] = -values[i];
				}																	
			} 
		}
		else
		{
			ArrayList<double[]> tmplist = new ArrayList<double[]>();
			
			for(int i = 0; i < values.length; i++)
			{
				double[] tmp = new double[2];
				tmp[0] = i;
				tmp[1] = values[i];
				tmplist.add(tmp);
			}
			
			Comparator<double[]> compValue = new ValueComparator();
			Collections.sort(tmplist, compValue);
					
			for(int i = 0, size = tmplist.size(); i < size; i++)
			{		
				if(leave < i)
				{
					double[] tmp = tmplist.get(i);
					tmp[1] = -tmp[1];
				}							    
			}
			Comparator<double[]> compLabel = new LabelComparator();
			Collections.sort(tmplist, compLabel);
			
			for(int i = 0; i < tmplist.size(); i++)
			{
				double[] tmp = tmplist.get(i);
				if(tmp[1] > 0)
				{
					sel[i] = -1;
				}
				else
				{
					sel[i] = i;	
				}
				val[i] = tmp[1];
			}
		}		
	}
}

class LabelComparator implements Comparator<double[]> 
{
    public int compare(double[] a, double[] b) 
    {
        if (a[0] < b[0]) 
        {
            return -1;
        } 
        else if (a[0] == b[0]) 
        {
            return 0;
        } 
        else 
        {
            return 1;
        }
    }
}

class ValueComparator implements Comparator<double[]> 
{
    public int compare(double[] a, double[] b) 
    {
        if (a[1] > b[1]) 
        {
            return -1;
        } 
        else if (a[1] == b[1]) 
        {
            return 0;
        } 
        else 
        {
            return 1;
        }
    }
}
